'use strict';

/**
 * @param {menu} menu menu数组
 * @param {target} target 需要高亮的目标
 * @return {menu} 将设置好的新menu返回
 */
function setMenuActive(menu, target) {
  if (menu && menu.length > 0) {
    menu.forEach(element => {
      if (element.label === target) {
        element.isActive = true;
      }

      if (element.children && element.children.length > 0) {
        element.children = setMenuActive(element.children, target);
      }
    });

    return menu;
  }
  return [];
}

/**
 * 获取首页快捷导航
 * @param {menu} menu 导航信息
 * @return {list} 快捷导航列表
 */
function getHomeList(menu) {
  let list = [];
  list = cycleMenu(menu);
  function cycleMenu(menu) {
    const tmp = [];
    if (menu && menu.length > 0) {
      menu.forEach(element => {
        if (element.children && element.children.length > 0) {
          tmp.concat(cycleMenu(element.children));
        }
      });
    }
    return tmp;
  }

  return list;
}

/**
 * @param {menu} menu menu数组和单个节点
 * @param {target} target 目标节点
 * @return {node} 选出的节点
 */
function findTargetHTMLType(menu, target) {
  let node = {};
  if (menu) {
    if (menu.length > 0) {
      menu.forEach(element => {
        if (element.label === target) {
          node = element;
        } else if (element.children && element.children.length >= 1) {
          element.children.forEach(ele => {
            if (ele.label === target) {
              node = ele;
            }
          });
        }
      });
    }
  }
  return node;
}

/**
 * @param {menu} menu menu数组
 * @param {target} target 目标sidebar数组
 * @return {result} 返回抽取的sidebar
 */
function getSideBar(menu, target) {
  let result = null;
  menu.forEach(element => {
    if (element.label === target) {
      result = element;
    }
  });

  return result;
}

/**
 * @param {typeid} typeId 模板选择id
 * @return {template} 返回对应的模板
 */
function selectTemplate(typeId) {
  let template = null;
  switch (parseInt(typeId)) {
    case 1:
      template = '../view/templates/list1.njk';
      break;
    case 2:
      template = '../view/templates/list2.njk';
      break;
    case 4:
      template = '../view/templates/detail1.njk';
      break;
    case 5:
      template = '../view/templates/detail2.njk';
      break;

    default:
      break;
  }

  return template;
}

/**
 * @param {obj} obj 需要校验的对象
 * @return {Boolean} 返回布尔值
 */
function isEmpltyObject(obj) {
  for (const key in obj) {
    return false;
  }
  return true;
}

module.exports = {
  setMenuActive,
  getSideBar,
  selectTemplate,
  findTargetHTMLType,
  isEmpltyObject,
  getHomeList,
};
